#include "showdata.h"

// 显示数据库数据
static void show_data(GtkWidget *list)
{
    sqlite3 *db;
    int ret = sqlite3_open("Data.db", &db);
    char **result, *errmsg;
    int nrow, ncolumn, i, j, index;

    if (sqlite3_get_table(db, "select * from persons order by id;", &result, &nrow, &ncolumn, &errmsg) != 0)
    {
        printf("error : %s\n", errmsg);
        sqlite3_free(errmsg);
    }

    // 清空原本列表中的数据
    gtk_clist_clear((GtkCList *)list);
    index = ncolumn;
    for (i = 0; i < nrow; i++)
    {
        gtk_clist_append((GtkCList *)list, result + index);
        for (j = 0; j < ncolumn; j++)
        {
            printf("%-8s", result[index]);
            index++;
        }
        printf("\n");
    }
    sqlite3_free_table(result);
    ret = sqlite3_close(db);

    return;
}

// 按id排序
static void id_sort(GtkWidget *widget, gpointer data)
{
    sqlite3 *db;
    static int id_flag = 0;
    int ret = sqlite3_open("Data.db", &db);
    char **result, *errmsg;
    int nrow, ncolumn, i, j, index;

    if (id_flag)
        ret = sqlite3_get_table(db, "select * from persons order by id;", &result, &nrow, &ncolumn, &errmsg);
    else
        ret = sqlite3_get_table(db, "select * from persons order by id desc;", &result, &nrow, &ncolumn, &errmsg);

    if (ret != SQLITE_OK)
    {
        printf("error : %s\n", errmsg);
        sqlite3_free(errmsg);
    }
    id_flag = !id_flag;

    // 清空原本列表中的数据
    gtk_clist_clear((GtkCList *)data);
    index = ncolumn;
    for (i = 0; i < nrow; i++)
    {
        gtk_clist_append((GtkCList *)data, result + index);
        for (j = 0; j < ncolumn; j++)
        {
            printf("%-8s", result[index]);
            index++;
        }
        printf("\n");
    }
    sqlite3_free_table(result);
    ret = sqlite3_close(db);
    return;
}

// 按名字排序
static void name_sort(GtkWidget *widget, gpointer data)
{
    sqlite3 *db;
    static int name_flag = 0;
    int ret = sqlite3_open("Data.db", &db);
    char **result, *errmsg;
    int nrow, ncolumn, i, j, index;

    if (name_flag)
        ret = sqlite3_get_table(db, "select * from persons order by name;", &result, &nrow, &ncolumn, &errmsg);
    else
        ret = sqlite3_get_table(db, "select * from persons order by name desc;", &result, &nrow, &ncolumn, &errmsg);

    if (ret != SQLITE_OK)
    {
        printf("error : %s\n", errmsg);
        sqlite3_free(errmsg);
    }
    name_flag = !name_flag;

    // 清空原本列表中的数据
    gtk_clist_clear((GtkCList *)data);
    index = ncolumn;
    for (i = 0; i < nrow; i++)
    {
        gtk_clist_append((GtkCList *)data, result + index);
        for (j = 0; j < ncolumn; j++)
        {
            printf("%-8s", result[index]);
            index++;
        }
        printf("\n");
    }
    sqlite3_free_table(result);
    ret = sqlite3_close(db);
    return;
}

// 按住址排序
static void addr_sort(GtkWidget *widget, gpointer data)
{
    sqlite3 *db;
    static int addr_flag = 0;
    int ret = sqlite3_open("Data.db", &db);
    char **result, *errmsg;
    int nrow, ncolumn, i, j, index;

    if (addr_flag)
        ret = sqlite3_get_table(db, "select * from persons order by addr;", &result, &nrow, &ncolumn, &errmsg);
    else
        ret = sqlite3_get_table(db, "select * from persons order by addr desc;", &result, &nrow, &ncolumn, &errmsg);

    if (ret != SQLITE_OK)
    {
        printf("error : %s\n", errmsg);
        sqlite3_free(errmsg);
    }

    addr_flag = !addr_flag;

    // 清空原本列表中的数据
    gtk_clist_clear((GtkCList *)data);
    index = ncolumn;
    for (i = 0; i < nrow; i++)
    {
        gtk_clist_append((GtkCList *)data, result + index);
        for (j = 0; j < ncolumn; j++)
        {
            printf("%-8s", result[index]);
            index++;
        }
        printf("\n");
    }
    sqlite3_free_table(result);
    ret = sqlite3_close(db);
    return;
}

GtkWidget *create_showdata_window()
{
    window_showdata = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    // 显示窗口标题
    gtk_window_set_title((GtkWindow *)window_showdata, "StuMgt学生信息管理系统");
    // 显示窗口大小
    gtk_widget_set_size_request(window_showdata, 800, 600);
    // 窗口位置设置：总是居中
    gtk_window_set_position((GtkWindow *)window_showdata, GTK_WIN_POS_CENTER_ALWAYS);
    // 窗口销毁 即程序结束
    g_signal_connect(window_showdata, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    /*--------------------------------容器---------------------------*/

    // 创建容器大小为1024*600
    GtkWidget *table = gtk_table_new(600, 800, TRUE);
    // 容器加入窗口
    gtk_container_add(GTK_CONTAINER(window_showdata), table);

    // 设置list显示数据
    int list_x = 200;
    int list_y = 50;
    int list_width = 550;
    int list_height = 400;
    char *title[] = {"id", "name", "addr", "sex"};
    GtkWidget *list = gtk_clist_new_with_titles(4, title);
    gtk_clist_set_column_width((GtkCList *)list, 0, 130);
    gtk_clist_set_column_width((GtkCList *)list, 1, 130);
    gtk_clist_set_column_width((GtkCList *)list, 2, 130);
    gtk_clist_set_column_width((GtkCList *)list, 3, 130);
    gtk_table_attach_defaults(GTK_TABLE(table), list, list_x, list_x + list_width, list_y, list_y + list_height);

    // 显示数据
    show_data(list);

    /*--------------------------------按钮---------------------------*/
    int start_x = 50;
    int end_x = 150;
    int stert_y = 50;
    int end_y = 80;

    // 显示数据按钮
    GtkWidget *id_sort_btn = gtk_button_new_with_label("通过id排序");
    // 把按钮加入布局
    gtk_table_attach_defaults(GTK_TABLE(table), id_sort_btn, start_x, end_x, stert_y, end_y);
    // 绑定信号
    g_signal_connect(id_sort_btn, "clicked", G_CALLBACK(id_sort), list);

    // 修改数据按钮
    GtkWidget *name_sort_btn = gtk_button_new_with_label("通过名字排序");
    // 把按钮加入布局
    gtk_table_attach_defaults(GTK_TABLE(table), name_sort_btn, start_x, end_x, stert_y + 100, end_y + 100);
    // 绑定信号
    g_signal_connect(name_sort_btn, "clicked", G_CALLBACK(name_sort), list);

    // 返回按钮
    GtkWidget *addr_sort_btn = gtk_button_new_with_label("通过地区排序");
    // 把按钮加入布局
    gtk_table_attach_defaults(GTK_TABLE(table), addr_sort_btn, start_x, end_x, stert_y + 200, end_y + 200);
    // 绑定信号
    g_signal_connect(addr_sort_btn, "clicked", G_CALLBACK(addr_sort), list);

    // 返回按钮
    GtkWidget *go_back_btn = gtk_button_new_with_label("返回");
    // 把按钮加入布局
    gtk_table_attach_defaults(GTK_TABLE(table), go_back_btn, start_x, end_x, stert_y + 400, end_y + 400);
    // 返回窗口
    g_signal_connect(go_back_btn, "clicked", G_CALLBACK(hide_window), window_showdata);
    g_signal_connect(go_back_btn, "clicked", G_CALLBACK(show_window), "window_operation");

    return window_showdata;
}